//
//  WaveProgress.m
//  WaveViewTest
//
//  Created by MorpLCP on 2017/8/14.
//  Copyright © 2017年 yizhan. All rights reserved.
//

#import "WaveProgress.h"
#import "CPWaveView.h"
#define WAVEHEIGHT 20

@interface WaveProgress ()

@property (nonatomic, strong) CPWaveView *waveView;
@property (nonatomic, strong) UILabel *progressLabel;
@property (nonatomic) CGMutablePathRef path;

@end

@implementation WaveProgress

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.value = 0;
        [self initSubViews];
        [self addCircleLayerMask];
    }
    return self;
}

- (void)initSubViews
{
    self.backgroundColor = [UIColor clearColor];
    self.clipsToBounds = YES;
    
    _waveView = [[CPWaveView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height + WAVEHEIGHT)];
    _waveView.topWaveColor = [UIColor clearColor];
    _waveView.bottomWaveColor = [UIColor greenColor];
    _waveView.waveHeight = WAVEHEIGHT;
    _waveView.waveSpeed = 0.5;
    [_waveView start];
    [self addSubview:_waveView];
    
    self.progressLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    self.progressLabel.textAlignment = NSTextAlignmentCenter;
    self.progressLabel.textColor = [UIColor whiteColor];
    self.progressLabel.font = [UIFont systemFontOfSize:30];
    [self addSubview:_progressLabel];
}

- (void)addCircleLayerMask
{
    // 心形
    //    CGFloat height = self.frame.size.height;
    //    CGFloat width = height;
    //    CGPoint leftCenter = CGPointMake(width * 0.25, height * 0.25);
    //    CGFloat leftRadius = height * 0.25;
    //    CGPoint rightCenter = CGPointMake(width * 0.75, leftCenter.y);
    //    CGFloat rightRadius = height * 0.25;
    //    // 绘制心形路径
    //    UIBezierPath *circlePath = [UIBezierPath bezierPathWithArcCenter:leftCenter radius:leftRadius startAngle:M_PI endAngle:0 clockwise:YES];
    //    [circlePath addArcWithCenter:rightCenter radius:rightRadius startAngle:M_PI endAngle:0 clockwise:YES];
    //    [circlePath addQuadCurveToPoint:CGPointMake(leftCenter.x + leftRadius, leftRadius * 4.0) controlPoint:CGPointMake(rightCenter.x + leftRadius, leftRadius * 2.0)];
    //    [circlePath addQuadCurveToPoint:CGPointMake(leftCenter.x - leftRadius, leftCenter.y) controlPoint:CGPointMake(leftCenter.x - leftRadius, leftCenter.y + leftRadius)];
    
    // 五角星
    //    CGFloat height = self.frame.size.height;
    //    CGFloat width = height;
    //    //确定中心点
    //    CGPoint centerPoint = CGPointMake(width * 0.5, height * 0.5);
    //    //确定半径
    //    CGFloat radius = width * 0.5f;
    //    UIBezierPath *circlePath = [UIBezierPath bezierPath];
    //    CGPoint p1 = CGPointMake(centerPoint.x, centerPoint.y - radius);
    //    [circlePath moveToPoint:p1];
    //    CGFloat angle = 4 * M_PI / 5.0;
    //    // 获取五个关键点
    //    for (int i = 1; i <= 5; i++)
    //    {
    //        CGFloat x = centerPoint.x - sinf(i * angle) * radius;
    //        CGFloat y = centerPoint.y - cosf(i * angle) * radius;
    //        [circlePath addLineToPoint:CGPointMake(x, y)];
    //    }
    //    [circlePath closePath];
    
    // 圆形
    UIBezierPath *circlePath = [UIBezierPath bezierPath];
    [circlePath addArcWithCenter:CGPointMake(self.frame.size.width * 0.5f, self.frame.size.height * 0.5f) radius:self.frame.size.width * 0.5f startAngle:0 endAngle:M_PI * 2 clockwise:YES];
    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.path = circlePath.CGPath;
    self.layer.mask = layer;
}

- (void)setValue:(CGFloat)value
{
    _value = value;
    [UIView animateWithDuration:0.5 animations:^{
        _waveView.frame = CGRectMake(0, self.frame.size.height - (-WAVEHEIGHT * 0.5 + (self.frame.size.height + WAVEHEIGHT) * value), self.frame.size.width, self.frame.size.height + WAVEHEIGHT);
    }];
    [self strockProgress];
}

- (void)strockProgress
{
    NSString *pro = [NSString stringWithFormat:@"%.f%%", self.value * 100];
    self.progressLabel.text = pro;
    //    CGRect rect = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    //    //段落格式
    //    NSMutableParagraphStyle *textStyle = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];
    //    textStyle.lineBreakMode = NSLineBreakByWordWrapping;
    //    textStyle.alignment = NSTextAlignmentCenter;//水平居中
    //    UIFont  *font = [UIFont boldSystemFontOfSize:30.0];
    //    NSDictionary *attributes = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:textStyle, NSForegroundColorAttributeName:[UIColor whiteColor]};
    //    CGSize strSize = [pro sizeWithAttributes:attributes];
    //    CGFloat marginTop = (rect.size.height - strSize.height) / 2;
    //    CGRect r = CGRectMake(rect.origin.x, rect.origin.y + marginTop,rect.size.width, strSize.height);
    //    [pro drawInRect:r withAttributes:attributes];
}

@end
